package com.guigu.service_user.controller;


import com.guigu.common_util.pojo.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common_util.pojo.utils.MD5;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.common_util.pojo.utils.ResultCode;
import com.guigu.service_user.entity.NewUser;
import com.guigu.service_user.entity.TbProvinces;
import com.guigu.service_user.entity.TbUser;
import com.guigu.service_user.service.ITbProvincesService;
import com.guigu.service_user.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@RestController
@RequestMapping("/serviceuser/tb-user")
public class TbUserController {
    @Autowired
    private ITbUserService userService;

    // 分页条件查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody(required = false) Map<String, Object> searchMap, Integer page, Integer size) {
        return userService.findPage(searchMap, page, size);
    }

    @GetMapping("sendMsm")
    public Result sendMsm(String phone){
        Result result = userService.sendMsm(phone);
        return result;
    }

    //增加
    @PostMapping("/insert")
    public Result insert(@RequestBody TbUser album) {
        boolean save = userService.save(album);
        if (save){
            return Result.ok();
        }
        return Result.error();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody TbUser album) {
        boolean update = userService.updateById(album);
        if (update) {
            return Result.ok();
        }
        return Result.error();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String id) {
        boolean remove = userService.removeById(id);
        if (remove){
            return Result.ok();
        }
        return Result.error();
    }
    //查询单个
    @GetMapping("/findById")
    public Result findById(String id) {
        TbUser album = userService.getById(id);
        if (album==null){
            return Result.error();
        }
        return Result.ok().data("user",album);
    }

    /**
     * 注册
     * @param user
     * @param smsCode
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody TbUser user, String smsCode) {
        //密码加密
        String newpassword = MD5.encrypt(user.getPassword());
        user.setPassword(newpassword);
        userService.save(user, smsCode);
        return Result.ok();
    }

    // 登录
    @PostMapping("/login")
    public Result loginUser(@RequestBody TbUser user) {
        // 调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = userService.login(user);
        return Result.ok().data("token", token);
    }

    // 根据token获取用户信息
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam String username) {
        // 查询数据库根据用户id获取用户信息
        TbUser user = userService.getById(username);
        return Result.ok().data("user",user);
    }



    // 分页条件查询
    @PostMapping("/findPages")
    public Result findPages(@RequestBody(required = false) Map<String, Object> searchMap, Integer page, Integer size) {
        return userService.selByCount(searchMap, page, size);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody NewUser newUser){
        boolean b = userService.removeById(newUser.getUsername());

        if(b){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("/deletelist")
    public Result deletelist(@RequestBody List<NewUser> list){
        Boolean b=false;
        for(NewUser o:list){
            b=userService.removeById(o.getUsername());
        }
        if(b){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("/updatenewUser")
    public Result updatenewUser(@RequestBody NewUser newUser){
        System.out.println(newUser);
        TbUser tbUser=new TbUser();
        if (newUser.getBirthday()!=null&&!"".equals(newUser.getBirthday())){
            tbUser.setBirthday(Instant.ofEpochMilli(newUser.getBirthday().getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
        if(newUser.getCreated()!=null&&!"".equals(newUser.getCreated())){
            tbUser.setCreated(Instant.ofEpochMilli(newUser.getCreated().getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
        if(newUser.getEmail()!=null&&!"".equals(newUser.getEmail())){
            tbUser.setEmail(newUser.getEmail());
        }
        if(newUser.getExperienceValue()!=null&&!"".equals(newUser.getExperienceValue())){
            tbUser.setExperienceValue(newUser.getExperienceValue());
        }
        if(newUser.getHeadPic()!=null&&!"".equals(newUser.getHeadPic())){
            tbUser.setHeadPic(newUser.getHeadPic());
        }
        if(newUser.getIsEmailCheck()!=null&&!"".equals(newUser.getIsEmailCheck())){
            tbUser.setIsEmailCheck(newUser.getIsEmailCheck());
        }
        if (newUser.getIsMobileCheck()!=null&&!"".equals(newUser.getIsMobileCheck())){
            tbUser.setIsMobileCheck(newUser.getIsMobileCheck());
        }
        if(newUser.getLastLoginTime()!=null&&!"".equals(newUser.getLastLoginTime())){
            tbUser.setLastLoginTime(Instant.ofEpochMilli(newUser.getLastLoginTime().getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
        if (newUser.getName()!=null&&!"".equals(newUser.getName())){
            tbUser.setName(newUser.getName());
        }
        if (newUser.getNickName()!=null&&!"".equals(newUser.getNickName())){
            tbUser.setNickName(newUser.getNickName());
        }
        if (newUser.getPassword()!=null&&!"".equals(newUser.getPassword())){
            tbUser.setPassword(MD5.encrypt(newUser.getPassword()));
        }
        if (newUser.getPhone()!=null&&!"".equals(newUser.getPhone())){
            tbUser.setPhone(newUser.getPhone());
        }
        if (newUser.getPoints()!=null&&!"".equals(newUser.getPoints())){
            tbUser.setPoints(newUser.getPoints());
        }
        if (newUser.getQq()!=null&&!"".equals(newUser.getQq())){
            tbUser.setQq(newUser.getQq());
        }
        if (newUser.getSex()!=null&&!"".equals(newUser.getSex())){
            tbUser.setSex(newUser.getSex());
        }
        if (newUser.getSourceType()!=null&&!"".equals(newUser.getSourceType())){
            tbUser.setSourceType(newUser.getSourceType());
        }
        if (newUser.getStatus()!=null&&!"".equals(newUser.getStatus())){
            tbUser.setStatus(newUser.getStatus());
        }
        if (newUser.getUserLevel()!=null&&!"".equals(newUser.getUserLevel())){
            tbUser.setUserLevel(newUser.getUserLevel());
        }
        if (newUser.getUsername()!=null&&!"".equals(newUser.getUsername())){
            tbUser.setUsername(newUser.getUsername());
        }
        if (newUser.getUpdated()!=null&&!"".equals(newUser.getUpdated())){
            tbUser.setUpdated(Instant.ofEpochMilli(newUser.getUpdated().getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
        boolean b = userService.updateById(tbUser);
        if(b){
            return Result.ok();
        }
        return Result.error();
    }




}

